//
//  BlutoothModel.m
//  FLORIDA
//
//  Created by 魏延(颜魏) on 2024/12/9.
//

#import "BlutoothGroupModel.h"

@implementation BlutoothGroupModel

- (NSDictionary *)toDict {
    NSMutableArray<NSDictionary *> *list = [NSMutableArray array];
    for (BlutoothModel *model in self.bluList) {
        [list addObject:[model toDict]];
    }
    return @{
        @"groupId": self.groupId,
        @"groupName": self.groupName,
        @"bluList": list,
    };
}

+ (NSString *)toJson:(NSArray<BlutoothGroupModel *> *)array {
    NSMutableArray *arr = [NSMutableArray array];
    for (BlutoothGroupModel *model in array) {
        [arr addObject:model.toDict];
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:arr options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (nonnull NSArray<BlutoothGroupModel *> *)jsonCovertToModel:(nonnull NSString *)json {
    NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&err];
//    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    NSMutableArray<BlutoothGroupModel *> *mlist = [NSMutableArray array];
    for (NSDictionary *dic in dataArray) {
        BlutoothGroupModel *model = [[BlutoothGroupModel alloc] init];
        model.groupId = [dic objectForKey:@"groupId"];
        model.groupName = [dic objectForKey:@"groupName"];
        
        NSArray *list = [dic objectForKey:@"bluList"];
        NSMutableArray<BlutoothModel *> *bList = [NSMutableArray array];
        for (NSDictionary *dict in list) {
            BlutoothModel *model = [[BlutoothModel alloc] init];
            model.cusName = [dict objectForKey:@"cusName"];
            model.name = [dict objectForKey:@"name"];
//            model.uuid = [dict objectForKey:@"uuid"];
            [bList addObject:model];
        }
        model.bluList = bList;
        [mlist addObject:model];
    }
    
    return mlist;
}

@end

@implementation BlutoothModel

- (NSDictionary *)toDict {
    return @{
        @"cusName": self.cusName ?: @"",
        @"name": self.name ?: @"",
//        @"uuid": self.uuid ?: @"",
    };
}

- (NSString *)toJson {
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self.toDict options:NSJSONWritingPrettyPrinted error:nil];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}

+ (BlutoothModel *)covertToModel:(NSString *)json {
    NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
    BlutoothModel *model = [[BlutoothModel alloc] init];
    model.cusName = [dic objectForKey:@"cusName"];
    model.name = [dic objectForKey:@"name"];
//    model.uuid = [dic objectForKey:@"uuid"];

    return model;
}


@end


